﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class SlideController : BaseController
    {

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Manager()
        {
            return View();
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult List()
        {
            return PartialView(RsDataManager.GetList<RedSand.Site.Models.SlideInfo>("SlideGetAll"));
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Create()
        {
            return PartialView();
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult Create(SlideInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(NewImage))
            {
                return AjaxMessage(Message.ImageUrlEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Image = SaveImage(null);
                if (RsDataManager.Save("SlideSave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<SlideInfo>("SlideGetInfo", "SlideID", id));
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(SlideInfo info)
        {
            if (ModelState.IsValid)
            {
                info.Image = SaveImage(info.Image);
                if (RsDataManager.Save("SlideSave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction]
        public ActionResult Delete(int id, string image)
        {
            if (RsDataManager.Delete("SlideDelete", "SlideID", id))
            {
                DeleteFile(image);
                AddMessage(Message.DeleteSuccess);
                return RedirectToAction("List");
            }
            return AjaxMessage(Message.DeleteError);
        }
    }
}
